<?PHP
/**
 * 批量自动确认和发货CRONTAB
 *
 * @author chenping@shopex.cn
 * @version $2012-8-13 15:33Z
 */
class wms_autotask_task_checkandconsign
{
    const BATCH_DIR = 'wms/delivery/checkandconsign/batch';

    function __construct($app)
    {
        $this->app = $app;
        $this->db = kernel::database();
    }

    /**
     * @description
     * @access public
     * @param void
     * @return void
     */
    public function error($log_id,$logi_no,$msg,$failNum)
    {
        $detail_log = array(
            'createtime' => time(),
            'logi_no' => $logi_no,
            'memo' => $msg,
            'status' => 'fail',
            'log_id' => $log_id,
        );

        $batchLog = app::get('wms')->model('batch_log');
        $batchLog->update(array('fail_number'=>$failNum),array('log_id'=>$log_id));

        $batchDetailLog = app::get('wms')->model('batch_detail_log');
        $batchDetailLog->insert($detail_log);

        // 从数组中剔除
        $key = array_search($logi_no,(array)$this->_log_text);
        if ($key !== false) {
            unset($this->_log_text[$key]);
        }
    }

    /**
     * @description
     * @access public
     * @param void
     * @return void
     */
    public function success($log_id,$logi_no,$succNum)
    {
        $data = array(
            'createtime' => time(),
            'logi_no' => $logi_no,
            'memo' => '发货成功',
            'status' => 'success',
            'log_id' => $log_id,
        );
        $batchDetailLog = app::get('wms')->model('batch_detail_log');
        $batchDetailLog->insert($data);

        // 从数组中剔除
        $key = array_search($logi_no,(array)$this->_log_text);
        if ($key !== false) {
            unset($this->_log_text[$key]);
        }
    }

    /**
     * @description 执行批量发货
     * @access public
     * @param void
     * @return void
     */
    public function process($params, &$error_msg='')
    {
        if( (!$params['log_id']) || (!$params['log_text']) ){
            return false;
        }else{
            $params['log_text'] = unserialize($params['log_text']);
        }

        set_time_limit(240);
        set_error_handler(array($this,'consign_error_handler'),E_USER_ERROR | E_ERROR);

        $this->exec_consign($params['log_id'],$params['log_text']);
        return  true;
    }

    /**
     * @description
     * @access public
     * @param void
     * @return void
     */
    public function exec_consign($log_id,$logiNoList,$loginfo = array())
    {

        $minWeight = app::get('wms')->getConf('wms.delivery.minWeight');
        #计算商品重量
        $orderObj = app::get('ome')->model('orders');
        $deliveryOrderObj = app::get('ome')->model('delivery_order');
        if (empty($logiNoList) || !is_array($logiNoList) || !$log_id) return false;


        $now = time();$logiNoList = array_filter($logiNoList);

        $deliBatchLog = app::get('wms')->model('batch_log');

        $operation = $deliBatchLog->select()->columns('op_id,op_name')->where('log_id=?',$log_id)->instance()->fetch_row();
        $op_id = $operation['op_id'];

        $fail = $loginfo['fail_number'] ? $loginfo['fail_number'] : 0;
        $succ = 0;


        $dlyObj = app::get('wms')->model('delivery');
        $dlyBillObj = app::get('wms')->model('delivery_bill');
        $deliveryBillLib = kernel::single('wms_delivery_bill');
        $opObj = app::get('ome')->model('operation_log');
        $wmsCommonLib = kernel::single('wms_common');
        $dlyProcessLib = kernel::single('wms_delivery_process');
        $dlyCheckLib = kernel::single('wms_delivery_check');
        $omeExtOrdLib = kernel::single('ome_extint_order');


        $carno = $logiNoList['carno'];

        foreach ($logiNoList['logi_no'] as $logi_no) {
            $logi_no = trim($logi_no);
            $this->_logino = $logi_no;
            $this->_log_id = $log_id;
            $this->_fail = $fail;
//            $bool = $this->checkandconsign($log_id, $logi_no['logi_no'], $logiNoList['logi_no']['carno']);


            $bool_delivery = false;
            $primary = false;
            $secondary = false;
            //如果没有发货单信息，则根据物流单号识别是主单还是次单,并获取相关信息
            $delivery_id = $deliveryBillLib->getDeliveryIdByPrimaryLogi($logi_no);

            if (!is_null($delivery_id)) {
                $primary = true;
                $dly = $dlyObj->dump(array('delivery_id' => $delivery_id), '*', array('delivery_items' => array('*')));
                $order_bns = $omeExtOrdLib->getOrderBns($dly['outer_delivery_bn']);
                $pause = $orderObj ->getList('pause',array('order_bns'=>$order_bns));
                foreach ($pause as $key => $value) {
                    if ($value['pause'] == 'true') {
                        $pausebool = true;
                        break;
                    }
                }
            }
            else {
                $delivery_id = $deliveryBillLib->getDeliveryIdBySecondaryLogi($logi_no);
                if (!is_null($delivery_id)) {
                    $secondary = true;
                    $dly = $dlyObj->dump(array('delivery_id' => $delivery_id), '*', array('delivery_items' => array('*')));
                    $order_bns = $omeExtOrdLib->getOrderBns($dly['outer_delivery_bn']);
                    $pause = $orderObj ->getList('pause',array('order_id'=>$order_bns));
                    foreach ($pause as $key => $value) {
                        if ($value['pause'] == 'true') {
                            $pausebool = true;
                            break;
                        }
                    }

                }

            }

            //判断$dly是否存在，不存在提示发货单不存在
            if (!$dly) {
                $msg =  '不存在该发货单不存在';
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }


            //重复发货判断
            if ($dly['status'] == 3 && $dly['process_status'] == 7) {
                $msg = $dly['delivery_bn'] . '重复发货！';
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }

            // 快递单未打印
            if(($dly['print_status'] & 4) != 4){
                $msg = $dly['delivery_bn'] . '快递单未打印！';
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }

            if (!$this->existOrderStatus($dly['delivery_id'])){
                $msg = '快递单号【'.$logi_no.'】对应发货单不处于可校验状态！';

                $msg = $dly['delivery_bn'] . $msg;
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }

            if (!$this->existOrderPause($dly['delivery_id'])){
                $msg = '快递单号【'.$logi_no.'】对应发货单订单存在异常！';

                $msg = $dly['delivery_bn'] . $msg;
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }

            if ($dly['status'] == 1){
                $msg = '快递单号【'.$logi_no.'】对应发货单已取消！';

                $msg = $dly['delivery_bn'] . $msg;
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }
            if ($dly['status'] == 2 || $pausebool){
                $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';

                $msg = $dly['delivery_bn'] . $msg;
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }
            if ($dly['status'] == 3){
                $msg = '快递单号【'.$logi_no.'】对应发货单已完成！';

                $msg = $dly['delivery_bn'] . $msg;
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
                continue;
            }
            //先校验
            if (($dly['process_status'] & 2) == 2){
                //已校验完成，直接发货
                //$msg = '快递单号【'.$logi_no.'】对应发货单已校验完成！';
            } else {
                $verify = $dlyProcessLib->verifyDelivery($delivery_id, true);
                if (!$verify) {
                    $msg = '物流单号:' . $dly['logi_no'] . '-发货单号:' . $dly['delivery_bn'] . '::校验失败';
                    $fail++;
                    $this->error($log_id, $logi_no, $msg, $fail);
                    continue;
                }
                usleep(200000);
            }
            if($dly['net_weight'] == 0){
                $MaterialExtObj = app::get('material')->model('basic_material');
                $basicMaterialExtObj = app::get('material')->model('basic_material_ext');
                foreach($dly['delivery_items'] as $key => $value){
                    $bm_id = $MaterialExtObj->dump(array('material_bn'=>$value['bn']),'bm_id');
                    $pkg = $basicMaterialExtObj->dump(array('bm_id'=>$bm_id['bm_id']),'weight');
                    $dly['net_weight']+=$pkg['weight']*$value['number'];
                }
            }
            $weight = $dly['net_weight'] ? $dly['net_weight'] : 0.00;
            $weight = $weight ? $weight : $minWeight;

            $logi_number = intval($dly['logi_number']);
            $delivery_logi_number = intval($dly['delivery_logi_number']);

            //检查前端订单是否退款,原有逻辑是否需要?

            //danny_freeze_stock_log
            define('FRST_TRIGGER_OBJECT_TYPE','发货单：批量发货');
            define('FRST_TRIGGER_ACTION_TYPE','ome_ctl_admin_consign：batchCheckConsign');

            //获取物流费用
            $area = $dly['consignee']['area'];
            $arrArea = explode(':', $area);
            $area_id = $arrArea[2];
            $delivery_cost_actual = $wmsCommonLib->getDeliveryFreight($area_id, $dly['logi_id'], $weight);
            //如果是次物流单号
            if ($secondary) {
                $data = array(
                    'status' => '1',
                    'weight' => $weight,
                    'delivery_cost_actual' => $delivery_cost_actual,
                    'delivery_time' => time(),
                    'type' => 2,
                );
                $filter = array('logi_no' => $logi_no);
                $dlyBillObj->update($data, $filter);

                $logstr = '批量发货,单号:' . $logi_no . ' 发货';
                $opObj->write_log('delivery_bill_express@wms', $dly["delivery_id"], $logstr);

                if (($logi_number == $delivery_logi_number) && $dly['status'] != '3') {
                    if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {

                        //判断是否调用物流回传接口
                        $Oshop = app::get('ome')->model('shop');
                        $shop = $Oshop->dump(array('shop_id' => $dly['shop_id']), 'isWD,shop_bn,is_sendshop');

                            if ($dly['status'] == 2){
                                $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';

                                $msg = $dly['delivery_bn'] . $msg;
                                $fail++;
                                $this->error($log_id,$logi_no,$msg,$fail);
                                continue;
                            }

                            /*
                            if ($shop['isWD'] == 'true' && $shop['is_sendshop'] == 'true') {
                                //调用回传物流信息的接口
                                $delivery_bn = $dly['delivery_bn'];
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $Oome_delivery_order = app::get('ome')->model('delivery_order');
                                $Oome_orders = app::get('ome')->model('orders');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']), 'delivery_id');
                                $ome_delivery_order = $Oome_delivery_order->dump(array('delivery_id' => $ome_delivery['delivery_id']), 'order_id');
                                $ome_orders = $Oome_orders->dump(array('order_id' => $ome_delivery_order['order_id']), 'relate_order_bn');
                                $order_bn = $ome_orders['relate_order_bn'];
                                $logi_no = $logi_no;
                                $logi_name = $dly['logi_name'];
                                $shop_bn = $shop['shop_bn'];
                                kernel::single('ome_UpdateAXStatus')->UpdateAXStatus($shop_bn, $delivery_bn, $order_bn, $logi_no, $logi_name);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }elseif($shop['isWD'] == 'false' && $shop['is_sendshop'] == 'true'){
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                                $Oome_delivery->call_delivery_api($ome_delivery['delivery_id'],false);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }
                            */


                        //修改OMS订单字段备货批号
                        $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                        $msgss = '发货处理完成';

                        $count = ""; //$dlyObj->countNoProcessDeliveryBill();
                        $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                        $succ++;
                        $this->success($log_id,$logi_no,$msg,$succ);

                    }
                    else {
                        $msg = $dly['delivery_bn'] . '发货未完成';
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                        continue;
                    }
                }
                else {
                    $data = array('delivery_logi_number' => $delivery_logi_number + 1, 'weight' => $dly['weight'] + $weight, 'delivery_cost_actual' => $dly['delivery_cost_actual'] + $delivery_cost_actual);
                    $filter = array('delivery_id' => $dly['delivery_id']);
                    $dlyObj->update($data, $filter);

                    if ($logi_number == ($delivery_logi_number + 1)) {
                        if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {

                             //判断是否调用物流回传接口
                            $Oshop = app::get('ome')->model('shop');
                            $shop = $Oshop->dump(array('shop_id' => $dly['shop_id']), 'isWD,shop_bn,is_sendshop');

                            if ($dly['status'] == 2){
                                $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';

                                $msg = $dly['delivery_bn'] . $msg;
                                $fail++;
                                $this->error($log_id,$logi_no,$msg,$fail);
                                continue;
                            }

                            /*
                            if ($shop['isWD'] == 'true' && $shop['is_sendshop'] == 'true') {
                                //调用回传物流信息的接口
                                $delivery_bn = $dly['delivery_bn'];
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $Oome_delivery_order = app::get('ome')->model('delivery_order');
                                $Oome_orders = app::get('ome')->model('orders');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']), 'delivery_id');
                                $ome_delivery_order = $Oome_delivery_order->dump(array('delivery_id' => $ome_delivery['delivery_id']), 'order_id');
                                $ome_orders = $Oome_orders->dump(array('order_id' => $ome_delivery_order['order_id']), 'relate_order_bn');
                                $order_bn = $ome_orders['relate_order_bn'];
                                $logi_no = $logi_no;
                                $logi_name = $dly['logi_name'];
                                $shop_bn = $shop['shop_bn'];
                                kernel::single('ome_UpdateAXStatus')->UpdateAXStatus($shop_bn, $delivery_bn, $order_bn, $logi_no, $logi_name);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }elseif($shop['isWD'] == 'false' && $shop['is_sendshop'] == 'true'){
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                                $Oome_delivery->call_delivery_api($ome_delivery['delivery_id'],false);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }
                            */

                            $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                            $msgss = '发货处理完成';

                            //$deliveryObj  = app::get('wms')->model('delivery');
                            $count = ""; //$dlyObj->countNoProcessDeliveryBill();
                            $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                            $succ++;
                            $this->success($log_id,$logi_no,$msg,$succ);
                            $bool_delivery = true;
                        } else {
                            $msg = $dly['delivery_bn'] . '发货未完成';
                            $fail++;
                            $this->error($log_id,$logi_no,$msg,$fail);
                            continue;
                        }
                    } else {
                        $count = ""; //$dlyObj->countNoProcessDeliveryBill();
                        $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                        $succ++;
                        $this->success($log_id,$logi_no,$msg,$succ);
                        $bool_delivery = true;
                    }
                }
            }
            else {
                //判断这个主物流单有没有对应的次物流单,等于1的时候只有一个包裹单
                if ($logi_number == 1) {
                    if (($logi_number == $delivery_logi_number) && $dly['status'] != '3') {
                        if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {

                             //判断是否调用物流回传接口
                            $Oshop = app::get('ome')->model('shop');
                            $shop = $Oshop->dump(array('shop_id' => $dly['shop_id']), 'isWD,shop_bn,is_sendshop');

                            if ($dly['status'] == 2){
                                $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';

                                $msg = $dly['delivery_bn'] . $msg;
                                $fail++;
                                $this->error($log_id,$logi_no,$msg,$fail);
                                continue;
                            }

                            /*
                            if ($shop['isWD'] == 'true' && $shop['is_sendshop'] == 'true') {
                                //调用回传物流信息的接口
                                $delivery_bn = $dly['delivery_bn'];
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $Oome_delivery_order = app::get('ome')->model('delivery_order');
                                $Oome_orders = app::get('ome')->model('orders');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']), 'delivery_id');
                                $ome_delivery_order = $Oome_delivery_order->dump(array('delivery_id' => $ome_delivery['delivery_id']), 'order_id');
                                $ome_orders = $Oome_orders->dump(array('order_id' => $ome_delivery_order['order_id']), 'relate_order_bn');
                                $order_bn = $ome_orders['relate_order_bn'];
                                $logi_no = $logi_no;
                                $logi_name = $dly['logi_name'];
                                $shop_bn = $shop['shop_bn'];
                                kernel::single('ome_UpdateAXStatus')->UpdateAXStatus($shop_bn, $delivery_bn, $order_bn, $logi_no, $logi_name);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }elseif($shop['isWD'] == 'false' && $shop['is_sendshop'] == 'true'){
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                                $Oome_delivery->call_delivery_api($ome_delivery['delivery_id'],false);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }
                            */

                            $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                            //$deliveryObj  = app::get('wms')->model('delivery');
                            $count = ""; //$dlyObj->countNoProcessDeliveryBill();

                            $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                            $succ++;
                            $this->success($log_id,$logi_no,$msg,$succ);
                            $bool_delivery = true;
                        } else {
                            $msg = $dly['delivery_bn'] . '发货未完成';
                            $fail++;
                            $this->error($log_id,$logi_no,$msg,$fail);
                            continue;
                        }
                    }
                    else {
                        $data = array(
                            'status' => '1',
                            'weight' => $weight,
                            'delivery_cost_actual' => $delivery_cost_actual,
                            'delivery_time' => time(),
                            'type' => 1,
                        );
                        $filter = array('logi_no' => $logi_no);
                        $dlyBillObj->update($data, $filter);
                        $data = array('delivery_logi_number' => $delivery_logi_number + 1, 'weight' => $dly['weight'] + $weight, 'delivery_cost_actual' => $dly['delivery_cost_actual'] + $delivery_cost_actual);
                        $filter = array('delivery_id' => $dly['delivery_id']);
                        $dlyObj->update($data, $filter);
                        if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {

                             //判断是否调用物流回传接口
                            $Oshop = app::get('ome')->model('shop');
                            $shop = $Oshop->dump(array('shop_id' => $dly['shop_id']), 'isWD,shop_bn,is_sendshop');

                            if ($dly['status'] == 2){
                                $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';

                                $msg = $dly['delivery_bn'] . $msg;
                                $fail++;
                                $this->error($log_id,$logi_no,$msg,$fail);
                                continue;
                            }

                            /*
                             if ($shop['isWD'] == 'true' && $shop['is_sendshop'] == 'true') {
                                //调用回传物流信息的接口
                                $delivery_bn = $dly['delivery_bn'];
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $Oome_delivery_order = app::get('ome')->model('delivery_order');
                                $Oome_orders = app::get('ome')->model('orders');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']), 'delivery_id');
                                $ome_delivery_order = $Oome_delivery_order->dump(array('delivery_id' => $ome_delivery['delivery_id']), 'order_id');
                                $ome_orders = $Oome_orders->dump(array('order_id' => $ome_delivery_order['order_id']), 'relate_order_bn');
                                $order_bn = $ome_orders['relate_order_bn'];
                                $logi_no = $logi_no;
                                $logi_name = $dly['logi_name'];
                                $shop_bn = $shop['shop_bn'];
                                kernel::single('ome_UpdateAXStatus')->UpdateAXStatus($shop_bn, $delivery_bn, $order_bn, $logi_no, $logi_name);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }elseif($shop['isWD'] == 'false' && $shop['is_sendshop'] == 'true'){
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                                $Oome_delivery->call_delivery_api($ome_delivery['delivery_id'],false);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }
                            */

                            $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");

                            //$deliveryObj  = app::get('wms')->model('delivery');
                            $count = ""; //$dlyObj->countNoProcessDeliveryBill();

                            $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                            $succ++;
                            $this->success($log_id,$logi_no,$msg,$succ);
                            $bool_delivery = true;
                        } else {
                            $msg = $dly['delivery_bn'] . '发货未完成';
                            $fail++;
                            $this->error($log_id,$logi_no,$msg,$fail);
                            continue;
                        }
                    }
                }
                else {
                    //如果存在子物流单
                    //计算已经发货的子物流单、总共的物流单
                    //1,查询实际的发货数量，和总物流数量
                    if (($logi_number > $delivery_logi_number)) {
                        $data = array(
                            'status' => '1',
                            'weight' => $weight,
                            'delivery_cost_actual' => $delivery_cost_actual,
                            'delivery_time' => time(),
                            'type' => 1,
                        );
                        $filter = array('logi_no' => $logi_no);
                        $dlyBillObj->update($data, $filter);
                        $data = array(
                            'delivery_logi_number' => $delivery_logi_number + 1,
                            'weight' => $dly['weight'] + $weight,
                            'delivery_cost_actual' => $dly['delivery_cost_actual'] + $delivery_cost_actual,
                        );
                        $filter = array('delivery_id' => $dly['delivery_id']);
                        $dlyObj->update($data, $filter);
                        if ($logi_number == ($delivery_logi_number + 1)) {
                            if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {

                                 //判断是否调用物流回传接口
                                $Oshop = app::get('ome')->model('shop');
                                $shop = $Oshop->dump(array('shop_id' => $dly['shop_id']), 'isWD,shop_bn,is_sendshop');

                                if ($dly['status'] == 2){
                                    $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';

                                    $msg = $dly['delivery_bn'] . $msg;
                                    $fail++;
                                    $this->error($log_id,$logi_no,$msg,$fail);
                                    continue;
                                }

                                /*
                                if ($shop['isWD'] == 'true' && $shop['is_sendshop'] == 'true') {
                                    //调用回传物流信息的接口
                                    $delivery_bn = $dly['delivery_bn'];
                                    $Oome_delivery = app::get('ome')->model('delivery');
                                    $Oome_delivery_order = app::get('ome')->model('delivery_order');
                                    $Oome_orders = app::get('ome')->model('orders');
                                    $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']), 'delivery_id');
                                    $ome_delivery_order = $Oome_delivery_order->dump(array('delivery_id' => $ome_delivery['delivery_id']), 'order_id');
                                    $ome_orders = $Oome_orders->dump(array('order_id' => $ome_delivery_order['order_id']), 'relate_order_bn');
                                    $order_bn = $ome_orders['relate_order_bn'];
                                    $logi_no = $logi_no;
                                    $logi_name = $dly['logi_name'];
                                    $shop_bn = $shop['shop_bn'];
                                    kernel::single('ome_UpdateAXStatus')->UpdateAXStatus($shop_bn, $delivery_bn, $order_bn, $logi_no, $logi_name);
                                    $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                                }elseif($shop['isWD'] == 'false' && $shop['is_sendshop'] == 'true'){
                                    $Oome_delivery = app::get('ome')->model('delivery');
                                    $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                                    $Oome_delivery->call_delivery_api($ome_delivery['delivery_id'],false);
                                    $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                                }
                                */


                                $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                                //$deliveryObj  = app::get('wms')->model('delivery');
                                $count = ""; //$dlyObj->countNoProcessDeliveryBill();

                                $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                                $succ++;
                                $this->success($log_id,$logi_no,$msg,$succ);
                                continue;
                            } else {
                                $msg = $dly['delivery_bn'] . '发货未完成';
                                $fail++;
                                $this->error($log_id,$logi_no,$msg,$fail);
                                continue;
                            }
                        }
                        //加入如果$logi_number==$delivery_logi_number 但是发货状态没有改变的判断
                    }
                    elseif (($delivery_logi_number == $logi_number) && $dly['status'] != 3) {
                        if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {

                             //判断是否调用物流回传接口
                            $Oshop = app::get('ome')->model('shop');
                            $shop = $Oshop->dump(array('shop_id' => $dly['shop_id']), 'isWD,shop_bn,is_sendshop');

                            if ($dly['status'] == 2){
                                $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';

                                $msg = $dly['delivery_bn'] . $msg;
                                $fail++;
                                $this->error($log_id,$logi_no,$msg,$fail);
                                continue;
                            }

                            /*
                            if ($shop['isWD'] == 'true' && $shop['is_sendshop'] == 'true') {
                                //调用回传物流信息的接口
                                $delivery_bn = $dly['delivery_bn'];
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $Oome_delivery_order = app::get('ome')->model('delivery_order');
                                $Oome_orders = app::get('ome')->model('orders');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']), 'delivery_id');
                                $ome_delivery_order = $Oome_delivery_order->dump(array('delivery_id' => $ome_delivery['delivery_id']), 'order_id');
                                $ome_orders = $Oome_orders->dump(array('order_id' => $ome_delivery_order['order_id']), 'relate_order_bn');
                                $order_bn = $ome_orders['relate_order_bn'];
                                $logi_no = $logi_no;
                                $logi_name = $dly['logi_name'];
                                $shop_bn = $shop['shop_bn'];
                                kernel::single('ome_UpdateAXStatus')->UpdateAXStatus($shop_bn, $delivery_bn, $order_bn, $logi_no, $logi_name);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }elseif($shop['isWD'] == 'false' && $shop['is_sendshop'] == 'true'){
                                $Oome_delivery = app::get('ome')->model('delivery');
                                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                                $Oome_delivery->call_delivery_api($ome_delivery['delivery_id'],false);
                                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
                            }
                            */

                            
                            $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");

                            //$deliveryObj  = app::get('wms')->model('delivery');
                            $count = ""; //$dlyObj->countNoProcessDeliveryBill();

                            $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                            $succ++;
                            $this->success($log_id,$logi_no,$msg,$succ);
                        } else {
                            $msg = $dly['delivery_bn'] . '发货未完成';
                            $fail++;
                            $this->error($log_id,$logi_no,$msg,$fail);
                        }
                    }
                    else {
                        $msg = $dly['delivery_bn'] . '此物流运单已发货';
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                    }
                }
            }

            usleep(200000);
        }
    }

    /**
     * @description
     * @access public
     * @param void
     * @return void
     */
    public function exec_consign_bak($log_id,$logiNoList,$loginfo = array())
    {
        $minWeight = app::get('wms')->getConf('wms.delivery.minWeight');
        #计算商品重量
        $orderObj = app::get('ome')->model('orders');
        $deliveryOrderObj = app::get('ome')->model('delivery_order');
        if (empty($logiNoList) || !is_array($logiNoList) || !$log_id) return false;

        $now = time();$logiNoList = array_filter($logiNoList);

        $deliBatchLog = app::get('wms')->model('batch_log');

        $operation = $deliBatchLog->select()->columns('op_id,op_name')->where('log_id=?',$log_id)->instance()->fetch_row();
        $op_id = $operation['op_id'];

        $deliBillModel = app::get('wms')->model('delivery_bill');
        $deliModel = app::get('wms')->model('delivery');
        $opLogModel = app::get('ome')->model('operation_log');

        $dlyCheckLib = kernel::single('wms_delivery_check');
        $dlyProcessLib = kernel::single('wms_delivery_process');
        $dlyBillLib = kernel::single('wms_delivery_bill');
        $wmsCommonLib = kernel::single('wms_common');


        $fail = $loginfo['fail_number'] ? $loginfo['fail_number'] : 0;
        $succ = 0;
        foreach ($logiNoList as $logi_no) {
            $logi_no = trim($logi_no);
            $this->_logino = $logi_no;
            $this->_log_id = $log_id;
            $this->_fail = $fail;

            $delivery_id = $dlyBillLib->getDeliveryIdByPrimaryLogi($logi_no);
            if(!is_null($delivery_id)){
                $primary = true;
                $dly = $deliModel->dump(array('delivery_id' => $delivery_id),'*',array('delivery_items'=>array('*')));
            }else{
                $delivery_id = $dlyBillLib->getDeliveryIdBySecondaryLogi($logi_no);
                if(!is_null($delivery_id)){
                    $secondary = true;
                    $dly = $deliModel->dump(array('delivery_id' => $delivery_id),'*',array('delivery_items'=>array('*')));
                }
            }

            $weight = $dly['net_weight'] ? $dly['net_weight'] : 0.00;
            $weight = $weight ? $weight : $minWeight;

            if($primary){
                //计算实际运费
                $area = $dly['consignee']['area'];
                $arrArea = explode(':', $area);
                $area_id = $arrArea[2];
                $delivery_cost_actual = $wmsCommonLib->getDeliveryFreight($area_id,$dly['logi_id'],$weight);
            }

            $delivery = $dlyCheckLib->consignAllow('', $logi_no, false, true);
            if ($delivery) {
                $fail++;
                $this->error($log_id,$logi_no,$delivery,$fail);
                continue;
            }

            $this->_delivery = $dly;

            //-- 扫描包裹等于快递单数量
            if ($dly['delivery_logi_number'] == $dly['logi_number'] && $dly['status']<> 3) {
                if (!$dlyProcessLib->consignDelivery($dly['delivery_id'], wms_const::__BATCH)) {
                    $fail++;
                    $this->error($log_id,$logi_no,$msg,$fail);
                    //$this->db->rollback();
                } else {
                    $succ++;
                    $this->success($log_id,$logi_no,$succ);
                    //$this->db->commit($transaction);
                }
                continue;
            }

            //更新发货包裹数
            $delivery_logi_number = $dly['delivery_logi_number'] + 1;
            $deliUpdate = array(
                'delivery_logi_number'=>$delivery_logi_number,
            );

            $deliFilter = array('delivery_id'=>$dly['delivery_id']);
            $deliModel->update($deliUpdate,$deliFilter);

            if ($secondary) {

                $data = array(
                    'status'=> 1,
                    'weight'=> 0.00,
                    'delivery_cost_actual'=> 0.00,
                    'delivery_time'=>$now,
                );
                $filter = array('logi_no'=>$logi_no);
                $deliBillModel->update($data,$filter);

                # 日志
                $logstr = '批量发货,单号:'.$logi_no;
                $opLogModel->write_log('delivery_bill_express@wms', $dly['delivery_id'], $logstr,$now,$operation);

                if($dly['logi_number'] == $delivery_logi_number){
                    define('FRST_TRIGGER_OBJECT_TYPE','发货单：批量发货');
                    define('FRST_TRIGGER_ACTION_TYPE','ome_ctl_admin_consign：batchConsign');
                    if (!$dlyProcessLib->consignDelivery($dly['delivery_id'], wms_const::__BATCH)) {
                        $msg = '物流单号:'.$dly['logi_no'].'-发货单号:'.$dly['delivery_bn'].'::'.$msg;
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                        //$this->db->rollback();
                    } else {
                        $succ++;
                        $this->success($log_id,$logi_no,$succ);
                        //$this->db->commit($transaction);
                    }
                } else {
                    # 部分发货
                    $succ++;
                    $this->success($log_id,$logi_no,$succ);
                    //$this->db->commit($transaction);
                }
            } else {
                $data = array(
                    'status'=> 1,
                    'weight'=> $weight,
                    'delivery_cost_actual'=> $delivery_cost_actual,
                    'delivery_time'=>$now,
                );
                $filter = array('logi_no'=>$logi_no);
                $deliBillModel->update($data,$filter);

                $deliUpdate = array(
                    'weight'=> $weight,
                    'delivery_cost_actual'=> $delivery_cost_actual,
                );
                $deliFilter = array('delivery_id'=>$dly['delivery_id']);
                $deliModel->update($deliUpdate,$deliFilter);

                if($dly['logi_number'] == $delivery_logi_number){
                    if (!$dlyProcessLib->consignDelivery($dly['delivery_id'], wms_const::__BATCH)) {
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                        //$this->db->rollback();
                    } else {
                        $succ++;
                        $this->success($log_id,$logi_no,$succ);
                        //$this->db->commit($transaction);
                    }
                } else {
                    $succ++;
                    $this->success($log_id,$logi_no,$succ);
                    //$this->db->commit($transaction);
                }
            }
            usleep(200000);
        }

        $deliBatchLog->update(array('status'=>'1','fail_number'=>$fail),array('log_id'=>$log_id));
    }


    /**
     * @description
     * @access public
     * @param void
     * @return void
     */
    public function exec_consign_bak1($log_id,$logiNoList,$loginfo = array())
    {
        $minWeight = app::get('wms')->getConf('wms.delivery.minWeight');
        #计算商品重量
        $orderObj = app::get('ome')->model('orders');
        $deliveryOrderObj = app::get('ome')->model('delivery_order');
        if (empty($logiNoList) || !is_array($logiNoList) || !$log_id) return false;

        $now = time();$logiNoList = array_filter($logiNoList);

        $deliBatchLog = app::get('wms')->model('batch_log');

        $operation = $deliBatchLog->select()->columns('op_id,op_name')->where('log_id=?',$log_id)->instance()->fetch_row();
        $op_id = $operation['op_id'];

        $deliBillModel = app::get('wms')->model('delivery_bill');
        $deliModel = app::get('wms')->model('delivery');
        $opLogModel = app::get('ome')->model('operation_log');

        $dlyCheckLib = kernel::single('wms_delivery_check');
        $dlyProcessLib = kernel::single('wms_delivery_process');
        $dlyBillLib = kernel::single('wms_delivery_bill');
        $wmsCommonLib = kernel::single('wms_common');


        $fail = $loginfo['fail_number'] ? $loginfo['fail_number'] : 0;
        $succ = 0;
        foreach ($logiNoList as $logi_no) {
            $logi_no = trim($logi_no);
            $this->_logino = $logi_no;
            $this->_log_id = $log_id;
            $this->_fail = $fail;

            $delivery_id = $dlyBillLib->getDeliveryIdByPrimaryLogi($logi_no);
            if(!is_null($delivery_id)){
                $primary = true;
                $dly = $deliModel->dump(array('delivery_id' => $delivery_id),'*',array('delivery_items'=>array('*')));
            }else{
                $delivery_id = $dlyBillLib->getDeliveryIdBySecondaryLogi($logi_no);
                if(!is_null($delivery_id)){
                    $secondary = true;
                    $dly = $deliModel->dump(array('delivery_id' => $delivery_id),'*',array('delivery_items'=>array('*')));
                }
            }

            $weight = $dly['net_weight'] ? $dly['net_weight'] : 0.00;
            $weight = $weight ? $weight : $minWeight;

            if($primary){
                //计算实际运费
                $area = $dly['consignee']['area'];
                $arrArea = explode(':', $area);
                $area_id = $arrArea[2];
                $delivery_cost_actual = $wmsCommonLib->getDeliveryFreight($area_id,$dly['logi_id'],$weight);
            }

            $delivery = $dlyCheckLib->consignAllow('', $logi_no, false, true);
            if ($delivery) {
                $fail++;
                $this->error($log_id,$logi_no,$delivery,$fail);
                continue;
            }

        $this->_delivery = $dly;

            //-- 扫描包裹等于快递单数量
            if ($dly['delivery_logi_number'] == $dly['logi_number'] && $dly['status']<> 3) {
                if (!$dlyProcessLib->consignDelivery($dly['delivery_id'], wms_const::__BATCH)) {
                    $fail++;
                    $this->error($log_id,$logi_no,$msg,$fail);
                    //$this->db->rollback();
                } else {
                    $succ++;
                    $this->success($log_id,$logi_no,$succ);
                    //$this->db->commit($transaction);
                }
                continue;
            }

            //更新发货包裹数
            $delivery_logi_number = $dly['delivery_logi_number'] + 1;
            $deliUpdate = array(
                'delivery_logi_number'=>$delivery_logi_number,
            );

            $deliFilter = array('delivery_id'=>$dly['delivery_id']);
            $deliModel->update($deliUpdate,$deliFilter);

            if ($secondary) {

                $data = array(
                    'status'=> 1,
                    'weight'=> 0.00,
                    'delivery_cost_actual'=> 0.00,
                    'delivery_time'=>$now,
                );
                $filter = array('logi_no'=>$logi_no);
                $deliBillModel->update($data,$filter);

                # 日志
                $logstr = '批量发货,单号:'.$logi_no;
                $opLogModel->write_log('delivery_bill_express@wms', $dly['delivery_id'], $logstr,$now,$operation);

                if($dly['logi_number'] == $delivery_logi_number){
                    define('FRST_TRIGGER_OBJECT_TYPE','发货单：批量发货');
                    define('FRST_TRIGGER_ACTION_TYPE','ome_ctl_admin_consign：batchConsign');
                    if (!$dlyProcessLib->consignDelivery($dly['delivery_id'], wms_const::__BATCH)) {
                        $msg = '物流单号:'.$dly['logi_no'].'-发货单号:'.$dly['delivery_bn'].'::'.$msg;
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                        //$this->db->rollback();
                    } else {
                        $succ++;
                        $this->success($log_id,$logi_no,$succ);
                        //$this->db->commit($transaction);
                    }
                } else {
                    # 部分发货
                    $succ++;
                    $this->success($log_id,$logi_no,$succ);
                    //$this->db->commit($transaction);
                }
            } else {
                $data = array(
                    'status'=> 1,
                    'weight'=> $weight,
                    'delivery_cost_actual'=> $delivery_cost_actual,
                    'delivery_time'=>$now,
                );
                $filter = array('logi_no'=>$logi_no);
                $deliBillModel->update($data,$filter);

                $deliUpdate = array(
                    'weight'=> $weight,
                    'delivery_cost_actual'=> $delivery_cost_actual,
                );
                $deliFilter = array('delivery_id'=>$dly['delivery_id']);
                $deliModel->update($deliUpdate,$deliFilter);

                if($dly['logi_number'] == $delivery_logi_number){
                    if (!$dlyProcessLib->consignDelivery($dly['delivery_id'], wms_const::__BATCH)) {
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                        //$this->db->rollback();
                    } else {
                        $succ++;
                        $this->success($log_id,$logi_no,$succ);
                        //$this->db->commit($transaction);
                    }
                } else {
                    $succ++;
                    $this->success($log_id,$logi_no,$succ);
                    //$this->db->commit($transaction);
                }
            }
            usleep(200000);
        }

        $deliBatchLog->update(array('status'=>'1','fail_number'=>$fail),array('log_id'=>$log_id));
    }

    /**
     * @description 获取操作员可执行的库存
     * @access public
     * @param void
     * @return void
     */
    public function getBranchByOp($op_id)
    {
        $bps = array();
        $oBops = $this->app->model('branch_ops');
        $bops_list = $oBops->getList('branch_id', array('op_id' => $op_id), 0, -1);
        if ($bops_list){
            $bps = array_map('current',$bops_list);
        }

        return $bps;
    }

    /**
     * @description 捕获发货异常信息
     * @access public
     * @param void
     * @return void
     */
    public function consign_error_handler($errno, $errstr, $errfile, $errline)
    {
        $batchLogModel = $this->app->model('batch_log');
        $batchLogModel->db->rollBack();

        $fail = $this->_fail+1;
        $this->error($this->_log_id,$this->_logino,$errstr,$fail);
        if ($this->_delivery['delivery_id']) {
            $deliModel = $this->app->model('delivery');
            $deliModel->update(array('delivery_logi_number'=>0),array('delivery_id'=>$this->_delivery['delivery_id']));
        }


        $log_text = serialize((array)$this->_log_text);
        if ($this->_log_id) {
            $data = array('log_text'=>$log_text);
            if (empty($this->_log_text)) {
                $data['status'] = '1';
            }
            $batchLogModel->update($data,array('log_id'=>$this->_log_id));
        }

        die(0);
    }



    private function checkandconsign($log_id, $logi_no, $carno, $weight =0){

        $dlyObj = app::get('wms')->model('delivery');
        $dlyBillObj = app::get('wms')->model('delivery_bill');
        $deliveryBillLib = kernel::single('wms_delivery_bill');
        $opObj = app::get('ome')->model('operation_log');
        $wmsCommonLib = kernel::single('wms_common');
        $dlyProcessLib = kernel::single('wms_delivery_process');

        $primary = false;
        $secondary = false;
        //如果没有发货单信息，则根据物流单号识别是主单还是次单,并获取相关信息
        $delivery_id = $deliveryBillLib->getDeliveryIdByPrimaryLogi($logi_no);

        if (!is_null($delivery_id)) {
            $primary = true;
            $dly = $dlyObj->dump(array('delivery_id' => $delivery_id), '*', array('delivery_items' => array('*')));

            //重复出货判断
            if ($dly['status'] == 3 && $dly['process_status'] == 7) {
                $msg = $dly['delivery_bn'] . '重复发货！';
                $fail++;
                $this->error($log_id,$logi_no,$msg,$fail);
            }
        }
        else {
            $delivery_id = $deliveryBillLib->getDeliveryIdBySecondaryLogi($logi_no);
            if (!is_null($delivery_id)) {
                $secondary = true;
                $dly = $dlyObj->dump(array('delivery_id' => $delivery_id), '*', array('delivery_items' => array('*')));

                //重复发货判断
                if ($dly['status'] == 3 && $dly['process_status'] == 7) {
                    $msg = $dly['delivery_bn'] . '重复发货！';
                    $fail++;
                    $this->error($log_id,$logi_no,$msg,$fail);
                }
            }

        }

        $logi_number = $dly['logi_number'];
        $delivery_logi_number = $dly['delivery_logi_number'];

        //检查前端订单是否退款,原有逻辑是否需要?

        //danny_freeze_stock_log
        define('FRST_TRIGGER_OBJECT_TYPE','发货单：批量发货');
        define('FRST_TRIGGER_ACTION_TYPE','ome_ctl_admin_consign：batchCheckConsign');

        //获取物流费用
        $area = $dly['consignee']['area'];
        $arrArea = explode(':', $area);
        $area_id = $arrArea[2];
        $delivery_cost_actual = $wmsCommonLib->getDeliveryFreight($area_id, $dly['logi_id'], $weight);

        //如果是次物流单号
        if ($secondary) {
            $data = array(
                'status' => '1',
                'weight' => $weight,
                'delivery_cost_actual' => $delivery_cost_actual,
                'delivery_time' => time(),
                'type' => 2,
            );
            $filter = array('logi_no' => $logi_no);
            $dlyBillObj->update($data, $filter);

            $logstr = '批量发货,单号:' . $logi_no . ' 发货';
            $opObj->write_log('delivery_bill_express@wms', $dly["delivery_id"], $logstr);

            if (($logi_number == $delivery_logi_number) && $dly['status'] != 3) {
                if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {
                    //修改OMS订单字段备货批号
                    $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                    $msgss = '发货处理完成';

                    $count = ""; //$dlyObj->countNoProcessDeliveryBill();
                    $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                    $succ++;
                    $this->success($log_id,$logi_no,$msg,$succ);

                } else {
                    $msg = $dly['delivery_bn'] . '发货未完成';
                    $fail++;
                    $this->error($log_id,$logi_no,$msg,$fail);
                }
            } else {
                $data = array('delivery_logi_number' => $delivery_logi_number + 1, 'weight' => $dly['weight'] + $weight, 'delivery_cost_actual' => $dly['delivery_cost_actual'] + $delivery_cost_actual);
                $filter = array('delivery_id' => $dly['delivery_id']);
                $dlyObj->update($data, $filter);

                if ($logi_number == ($delivery_logi_number + 1)) {
                    if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {
                        $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                        $msgss = '发货处理完成';

                        //$deliveryObj  = app::get('wms')->model('delivery');
                        $count = ""; //$dlyObj->countNoProcessDeliveryBill();
                        $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                        $succ++;
                        $this->success($log_id,$logi_no,$msg,$succ);

                    } else {
                        $msg = $dly['delivery_bn'] . '发货未完成';
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                    }
                } else {
                    $count = $dlyObj->countNoProcessDeliveryBill();
                    $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                    $succ++;
                    $this->success($log_id,$logi_no,$msg,$succ);
                }
            }
        }
        else {
            //判断这个主物流单有没有对应的次物流单,等于1的时候只有一个包裹单
            if ($logi_number == 1) {
                if (($logi_number == $delivery_logi_number) && $dly['status'] != 3) {
                    if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {
                        $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                        //$deliveryObj  = app::get('wms')->model('delivery');
                        $count = $dlyObj->countNoProcessDeliveryBill();

                        $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                        $succ++;
                        $this->success($log_id,$logi_no,$msg,$succ);

                    } else {
                        $msg = $dly['delivery_bn'] . '发货未完成';
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                    }
                } else {
                    $data = array(
                        'status' => '1',
                        'weight' => $weight,
                        'delivery_cost_actual' => $delivery_cost_actual,
                        'delivery_time' => time(),
                        'type' => 1,
                    );
                    $filter = array('logi_no' => $logi_no);
                    $dlyBillObj->update($data, $filter);
                    $data = array('delivery_logi_number' => $delivery_logi_number + 1, 'weight' => $dly['weight'] + $weight, 'delivery_cost_actual' => $dly['delivery_cost_actual'] + $delivery_cost_actual);
                    $filter = array('delivery_id' => $dly['delivery_id']);
                    $dlyObj->update($data, $filter);

                    if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {
                        $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");

                        //$deliveryObj  = app::get('wms')->model('delivery');
                        $count = $dlyObj->countNoProcessDeliveryBill();

                        $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                        $succ++;
                        $this->success($log_id,$logi_no,$msg,$succ);

                    } else {
                        $msg = $dly['delivery_bn'] . '发货未完成';
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                    }
                }
            } else {
                //如果存在子物流单
                //计算已经发货的子物流单、总共的物流单
                //1,查询实际的发货数量，和总物流数量
                if (($logi_number > $delivery_logi_number)) {
                    $data = array(
                        'status' => '1',
                        'weight' => $weight,
                        'delivery_cost_actual' => $delivery_cost_actual,
                        'delivery_time' => time(),
                        'type' => 1,
                    );
                    $filter = array('logi_no' => $logi_no);
                    $dlyBillObj->update($data, $filter);
                    $data = array(
                        'delivery_logi_number' => $delivery_logi_number + 1,
                        'weight' => $dly['weight'] + $weight,
                        'delivery_cost_actual' => $dly['delivery_cost_actual'] + $delivery_cost_actual,
                    );
                    $filter = array('delivery_id' => $dly['delivery_id']);
                    $dlyObj->update($data, $filter);
                    if ($logi_number == ($delivery_logi_number + 1)) {
                        if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {
                            $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");
                            //$deliveryObj  = app::get('wms')->model('delivery');
                            $count = $dlyObj->countNoProcessDeliveryBill();

                            $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                            $succ++;
                            $this->success($log_id,$logi_no,$msg,$succ);
                        } else {
                            $msg = $dly['delivery_bn'] . '发货未完成';
                            $fail++;
                            $this->error($log_id,$logi_no,$msg,$fail);
                        }
                    }
                    //加入如果$logi_number==$delivery_logi_number 但是发货状态没有改变的判断
                } elseif (($delivery_logi_number == $logi_number) && $dly['status'] != 3) {
                    if ($dlyProcessLib->consignDelivery($dly['delivery_id'])) {


                        $dlyObj->db->exec("update sdb_wms_delivery set carno = '" . $carno . "' where  delivery_id in(" . $dly['delivery_id'] . ")");

                        //$deliveryObj  = app::get('wms')->model('delivery');
                        $count = $dlyObj->countNoProcessDeliveryBill();

                        $msg = $dly['delivery_bn'] . '发货处理完成|' . $count;
                        $succ++;
                        $this->success($log_id,$logi_no,$msg,$succ);
                    } else {
                        $msg = $dly['delivery_bn'] . '发货未完成';
                        $fail++;
                        $this->error($log_id,$logi_no,$msg,$fail);
                    }
                } else {
                    $msg = $dly['delivery_bn'] . '此物流运单已发货';
                    $fail++;
                    $this->error($log_id,$logi_no,$msg,$fail);
                }
            }
        }
    }

    /**
     * @description 是否允许校验
     * @access public
     * @param void
     * @return void
     */
    public function checkAllow($logi_no, &$msg, $act,$command = false)
    {
        $dlyModel = app::get('wms')->model('delivery');
        $dlyItemsModel = app::get('wms')->model('delivery_items');
        $dlyBillObj = kernel::single('wms_delivery_bill');

        $delivery_id = $dlyBillObj->getDeliveryIdByPrimaryLogi($logi_no);
        $delivery = $dlyModel->getList('branch_id,delivery_id,status,deli_cfg,process_status,delivery_bn,print_status',array('delivery_id'=>$delivery_id),0,1);

        if (!$delivery) {
            $msg = '快递单号【'.$logi_no.'】不存在！';
            return false;
        }
        $delivery = current($delivery);

        if($command === false){
            $oBranch = app::get('ome')->model('branch');
            $is_super = kernel::single('desktop_user')->is_super();
            if ($is_super) {
                $branches = array('_ALL_');
            } else {
                $branches = $oBranch->getBranchByUser(true);
            }

            if (!in_array($delivery['branch_id'],$branches) && $branches[0] != '_ALL_') {
                $msg = '你无权对快递单【'.$logi_no.'】进行校验！';
                return false;
            }
        }

        if(!$act || !in_array($act,array('barcode','all','group','batch'))){
            $msg = '快递单号【'.$logi_no.'】无效的校验操作！';
            return false;
        }else{
            if(in_array($act,array('all','group','batch'))){
                $has_expire_bns = $dlyItemsModel->getList('item_id',array('delivery_id'=>$delivery_id,'use_expire'=>1));
                if($has_expire_bns){
                    $msg = '快递单号【'.$logi_no.'】存在保质期物料，推荐逐单校验！';
                    return false;
                }
            }
        }

        if (!$this->existOrderStatus($delivery['delivery_id'])){
            $msg = '快递单号【'.$logi_no.'】对应发货单不处于可校验状态！';
            return false;
        }

        if (!$this->existOrderPause($delivery['delivery_id'])){
            $msg = '快递单号【'.$logi_no.'】对应发货单订单存在异常！';
            return false;
        }

        if (($delivery['process_status'] & 2) == 2){
            $msg = '快递单号【'.$logi_no.'】对应发货单已校验完成！';
            return false;
        }
        if ($delivery['status'] != 0){
            $msg = '快递单号【'.$logi_no.'】对应发货单不满足校验需求！';
            return false;
        }
        if ($delivery['status'] == 2){
            $msg = '快递单号【'.$logi_no.'】对应发货单已暂停！';
            return false;
        }

        $printFinish = $this->checkPrintFinish($delivery,$print_msg);
        if($printFinish == false){
            $msg = $print_msg[0]['msg'];
            return false;
        }

        return true;
    }

    /**
     * 判断发货单状态是否存在异常
     *
     * @param int $dly_id
     * @return boolean
     */
    function existOrderStatus($dly_id){
        $ids = $dly_id;

        $sql = "SELECT COUNT(*) AS '_count'  FROM sdb_wms_delivery WHERE delivery_id in ($ids) AND (status=1 OR status=2 OR disabled='true')";
        $row = kernel::database()->select($sql);
        if ($row[0]['_count'] > 0){
            return false;
        }else {
            return true;
        }
    }

    /**
     *
     * 判断订单状态是否存在异常
     * @param int $dly_id
     */
    function existOrderPause($dly_id){
        $dlyObj = app::get('wms')->model('delivery');
        $omeExtOrdLib = kernel::single('ome_extint_order');

        $dlyInfo = $dlyObj->dump($dly_id,'outer_delivery_bn');
        if($dlyInfo){
            if($omeExtOrdLib->existOrderPause($dlyInfo['outer_delivery_bn'])){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
        return true;
    }

    /**
     * 检查发货单是否已经打印完成
     *
     * @author chenping<chenping@shopex.cn>
     * @version 2012-5-15 00:14
     * @param Array $dly 发货单信息 $dly
     * @param Array $msg 错误信息
     * @return TRUE:打印完成、FALSE:打印未完成
     **/
    public function checkPrintFinish($dly,&$msg){
        $deliCfgLib = kernel::single('wms_delivery_cfg');
        if($deliCfgLib->deliveryCfg != '') {
            $btncombi = $deliCfgLib->btnCombi($dly['deli_cfg']);
            list($stock,$delie) = explode('_',$btncombi);
            if(1 == $stock){
                if(($dly['print_status'] & 1) != 1) {
                    $msg[] = array('bn'=>$dly['logi_no'],'msg' => '备货单未打印');
                    return false;
                }
            }
            if(1 == $delie){
                if(($dly['print_status'] & 2) != 2){
                    $msg[] = array('bn' => $dly['logi_no'],'msg'=> '发货单未打印');
                    return false;
                }
            }
        }else{
            # 默认情况全部开启
            if(($dly['print_status'] & 1) != 1){      // 备货单未打印
                $msg[] = array('bn'=> $dly['logi_no'], 'msg'=> '备货单未打印');
                return false;
            }
            if(($dly['print_status'] & 2) != 2){     // 发货单未打印
                $msg[] = array('bn' => $dly['logi_no'], 'msg'=> '发货单未打印');
                return false;
            }
        }
        if(($dly['print_status'] & 4) != 4){   // 快递单未打印
            $msg[] = array('bn'=> $dly['logi_no'], 'msg'=> '快递单未打印');
            return false;
        }
        return true;
    }
}